<?php
    // +----------------------------------------------------------------------
    // | 模型基类
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------
    namespace app\common\model;

    use think\facade\Lang;
    use think\Model;
    use think\Container;
    use think\db\Where;

    class Base extends Model
    {
        protected static $request;

        /**
         * 初始化处理
         *
         * @access protected
         * @return void
         */
        protected static function init()
        {
            self::$request = app('request');
        }
		/**
		 * 数据自动完成
		 * @access protected
		 * @param  array $auto 要自动更新的字段列表
		 * @return void
		 */
		protected function autoCompleteData($auto = [])
		{
			foreach ($auto as $field => $value) {
				if (is_integer($field)) {
					$field = $value;
					$value = null;
				}

				if (!isset($this->data[$field])) {
					$default = null;
				} else {
					$default = $this->data[$field];
				}

				$this->setAttr($field, !is_null($value) ? $value : $default);
			}
		}

        /**
         * 通用更新方法
         * @param array   $param
         * @param unknown $user
         * @return array
         * @throws Exception
         */
        public function commonUpdateData($param = [], $user = null)
        {
            try {
                if (empty($param)) {
                    throw new \Exception(Lang::get('__data_format_error_'));
                }
                if (!empty($param[$this->getPk()])) {
                    // 更新操作
                    $map[$this->getPk()] = $param[$this->getPk()];
                    $result = $this->save($param, $map);
                } else { // 新增
                    $result = $this->create($param);
                }
                $data['code'] = 0;
                $data['data'] = $result;
                $data['message'] = '操作成功';
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }


    }
